package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Event;
import cn.edu.scau.cmi.ema.domain.Eventquantitytype;
import cn.edu.scau.cmi.ema.domain.Quantitytype;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("EventquantitytypeController")
public class EventquantitytypeController extends CmiEmaController {

	@RequestMapping("/listEventquantitytypes")
	public ModelAndView listEventquantitytypes(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = eventquantitytypePaginationService.updatePagination(request, pagination);
		Set<Eventquantitytype> eventquantitytypes = eventquantitytypePaginationService
				.getPaginationEventquantitytypes(pagination);

		mav.addObject("eventquantitytypes", eventquantitytypes);
		mav.addObject("entityName", "eventquantitytype");
		mav.addObject("pagination", pagination);
		mav.setViewName("eventquantitytype/listEventquantitytypes");
		return mav;
	}

	public String listEventquantitytypes() {
		return "redirect:/listEventquantitytypes";
	}

	@RequestMapping("/viewEventquantitytype")
	public ModelAndView viewEventquantitytype(@RequestParam Integer eventquantitytypeId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("eventquantitytype", eventquantitytypeDAO.findEventquantitytypeByPrimaryKey(eventquantitytypeId));
		mav.setViewName("eventquantitytype/viewEventquantitytype");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteEventquantitytypes")
	public ModelAndView confirmDeleteEventquantitytypes(
			@RequestParam(value = "deleteEventquantitytypesIds", required = true) Set<Integer> deleteEventquantitytypesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Eventquantitytype> deleteEventquantitytypes = eventquantitytypeDAO
				.findEventquantitytypesByIds(deleteEventquantitytypesIds);
		Set<Eventquantitytype> referencedEventquantitytypes = eventquantitytypeService
				.loadReferenceEventquantitytypes(deleteEventquantitytypes);
		boolean hasReferenced;
		if (referencedEventquantitytypes.size() == 0) {
			mav.addObject("deleteEntitys", deleteEventquantitytypes);
			hasReferenced = false;
		} else {
			deleteEventquantitytypes.removeAll(referencedEventquantitytypes);
			mav.addObject("referencedEntitys", referencedEventquantitytypes);
			mav.addObject("deleteEntitys", deleteEventquantitytypes);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Eventquantitytype");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listEventquantitytypes");
		return mav;
	}

	@RequestMapping("/deleteEventquantitytype")
	public String deleteEventquantitytype(@RequestParam Integer eventquantitytypeId) {
		Eventquantitytype eventquantitytype = eventquantitytypeDAO
				.findEventquantitytypeByPrimaryKey(eventquantitytypeId);
		eventquantitytypeService.deleteEventquantitytype(eventquantitytype);
		return "forward:/listEventquantitytypes";
	}

	@RequestMapping("/confirmDeleteEventquantitytype")
	public ModelAndView confirmDeleteEventquantitytype(@RequestParam Integer eventquantitytypeId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("eventquantitytype", eventquantitytypeDAO.findEventquantitytypeByPrimaryKey(eventquantitytypeId));

		mav.setViewName("eventquantitytype/deleteEventquantitytype");

		return mav;
	}

	@RequestMapping("/deleteEventquantitytypes")
	public String deleteEventquantitytypes(
			@RequestParam(value = "deleteEventquantitytypesId", required = true) Set<Integer> deleteEventquantitytypesId) {
		eventquantitytypeService.deleteEventquantitytypes(deleteEventquantitytypesId);
		return "forward:/listEventquantitytypes";
	}

	@RequestMapping("/editEventquantitytype")
	public ModelAndView editEventquantitytype(@RequestParam Integer eventquantitytypeId) {

		ModelAndView mav = new ModelAndView();

		Eventquantitytype eventquantitytype = eventquantitytypeDAO
				.findEventquantitytypeByPrimaryKey(eventquantitytypeId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("eventquantitytype", eventquantitytype);

		// 把所有的引用eventquantitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventquantitytypeService.loadEventquantitytypeObjectReferencedSet(eventquantitytype));

		// 将编辑页面eventquantitytype添加到mav中
		mav.setViewName("eventquantitytype/editEventquantitytype");
		return mav;
	}

	@RequestMapping("/newEventquantitytype")
	public ModelAndView newEventquantitytype() {
		ModelAndView mav = new ModelAndView();
		Eventquantitytype eventquantitytype = new Eventquantitytype();

		mav.addObject("eventquantitytype", eventquantitytype);

		// 把所有的引用eventquantitytype对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(eventquantitytypeService.loadEventquantitytypeObjectReferencedSet(eventquantitytype));

		mav.addObject("newFlag", true);
		mav.setViewName("eventquantitytype/editEventquantitytype");

		return mav;
	}

	@RequestMapping("/saveEventquantitytype")
	public ModelAndView saveEventquantitytype(@ModelAttribute Eventquantitytype eventquantitytype) {

		ModelAndView mav = new ModelAndView();

		Event eventTemp = eventquantitytype.getEvent();
		Event event = eventTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(eventDAO.findEventById(eventTemp.getId()));
		Quantitytype quantitytypeTemp = eventquantitytype.getQuantitytype();
		Quantitytype quantitytype = quantitytypeTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(quantitytypeDAO.findQuantitytypeById(quantitytypeTemp.getId()));

		Eventquantitytype existingEventquantitytype = CmiHibernateProxyUtil
				.deproxy(eventquantitytypeDAO.findEventquantitytypeByPhysicalPrimaryKey(event.getId(),
						quantitytype.getId(), eventquantitytype.getQuantity()));

		eventquantitytype.setEvent(event);
		eventquantitytype.setQuantitytype(quantitytype);
		// 新建记录
		if (eventquantitytype.getId() == null) {
			if (eventquantitytype.equals(existingEventquantitytype)) {

				mav.addAllObjects(eventquantitytypeService.loadEventquantitytypeClassReferencedSet(eventquantitytype));
				// mav = editContinue(mav, eventquantitytype);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventquantitytype/editEventquantitytype");
			} else {
				eventquantitytypeService.saveEventquantitytype(eventquantitytype);
				mav.setViewName("forward:/listEventquantitytypes");
			}
		}
		// 编辑记录
		else {
			Eventquantitytype originEventquantitytype = CmiHibernateProxyUtil
					.deproxy(eventquantitytypeDAO.findEventquantitytypeByPrimaryKey(eventquantitytype.getId()));
			if (!eventquantitytype.equals(originEventquantitytype)
					&& eventquantitytype.equals(existingEventquantitytype)) {
				mav.addAllObjects(eventquantitytypeService.loadEventquantitytypeClassReferencedSet(eventquantitytype));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("eventquantitytype/editEventquantitytype");
			} else {
				eventquantitytypeService.saveEventquantitytype(eventquantitytype);
				mav.setViewName("forward:/listEventquantitytypes");
			}
		}
		return mav;
	}

	@RequestMapping("/eventquantitytypeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
